﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using WaterCloud.Code;
using SqlSugar;
using WaterCloud.DataBase;
using WaterCloud.Domain.RecruitmentManagement;
using WaterCloud.Domain.SystemOrganize;
using WaterCloud.Domain.SystemManage;

namespace WaterCloud.Service.RecruitmentManagement
{
    /// <summary>
    /// 创 建：超级管理员
    /// 日 期：2025-04-03 10:48
    /// 描 述：人才库服务类
    /// </summary>
    public class PersonnelService : BaseService<PersonnelEntity>, IDenpendency
    {
        public PersonnelService(ISqlSugarClient context) : base(context)
        {
        }
        #region 获取数据
        public async Task<List<PersonnelEntity>> GetList(string keyword = "")
        {
            var data = repository.IQueryable();
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.Where(a => a.F_MobilePhone.Contains(keyword)
                || a.F_RealName.Contains(keyword));
            }
            return await data.Where(a => a.F_DeleteMark == false).OrderBy(a => a.F_Id , OrderByType.Desc).ToListAsync();
        }

        public async Task<List<PersonnelEntity>> GetLookList(string keyword = "")
        {
            var query = repository.IQueryable().Where(a => a.F_DeleteMark == false);
            if (!string.IsNullOrEmpty(keyword))
            {
                //此处需修改
                query = query.Where(a => a.F_MobilePhone.Contains(keyword)
                || a.F_RealName.Contains(keyword));
            }
             //权限过滤
             query = GetDataPrivilege("a", "", query);
             return await query.OrderBy(a => a.F_Id , OrderByType.Desc).ToListAsync();
        }

        public async Task<List<PersonnelEntity>> GetLookList(SoulPage<PersonnelEntity> pagination,int zt,string keyword = "",string id="")
        {
           
            var query = repository.Db.Queryable<PersonnelEntity, RecruitmentplanEntity> ((a, b) => new JoinQueryInfos(
            JoinType.Left, a.F_PlanId == b.F_Id
           
            
            )).Where(a=>a.F_State==zt)
            .Select((a, b) => new PersonnelEntity
            {
                F_Id = a.F_Id.SelectAll(),
             
              
           

            }).MergeTable();

            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(a => a.F_MobilePhone.Contains(keyword)
                || a.F_RealName.Contains(keyword));
            }
            if(!string.IsNullOrEmpty(id))
            {
                query= query.Where(a=>a.F_Id==id);
            }
            //权限过滤
            query = GetDataPrivilege("a","",query);
            return  await query.ToPageListAsync(pagination);
        }

        public async Task<PersonnelEntity> GetForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
           
            return data;
        }
        #endregion

        public async Task<PersonnelEntity> GetLookForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            data.F_PlanName = Db.Queryable<RecruitmentplanEntity>().Where(a => a.F_Id == data.F_PlanId).ToList()[0].F_FullName;
            data.F_OrganizeName= Db.Queryable<OrganizeEntity>().Where(a => a.F_Id == data.F_OrganizeId).ToList()[0].F_FullName;
            return GetFieldsFilterData(data);
        }

        #region 提交数据
        public async Task SubmitForm(PersonnelEntity entity, string keyValue)
        {
            if(string.IsNullOrEmpty(keyValue))
            {
                    //初始值添加
                entity.F_DeleteMark = false;
                entity.F_State = 0;
                entity.Create();
                await repository.Insert(entity);
            }
            else
            {
                    //此处需修改
                entity.Modify(keyValue); 
                await repository.Update(entity);
            }
        }

        public async Task DeleteForm(string keyValue)
        {
            var ids = keyValue.Split(',');
            await repository.Delete(a => ids.Contains(a.F_Id.ToString()));
        }
        public async Task StateForm(string keyValue,int zt)
        {
            var ids = keyValue.Split(',');
            await repository.Update(a => ids.Contains(a.F_Id), a => new PersonnelEntity
            {
                F_State = zt,
               

            });

            if (zt == 3)
            {
                List<UserEntity> list = new List<UserEntity>();
                var data = repository.IQueryable().Where(a => ids.Contains(a.F_Id)).ToList();
                foreach (var item in data)
                {
                    UserEntity user = new UserEntity();
                   user.F_Id= Utils.GuId();
                    user.F_ImgUrl = item.F_ImgUrl;
                    user.F_RealName = item.F_RealName;
                    user.F_PlanId = item.F_PlanId;
                    user.F_Gender = item.F_Gender;
                    user.F_Birthday = item.F_Birthday;
                    user.F_Certificate = item.F_Certificate;
                    user.F_CertificateNum = item.F_CertificateNum;
                    user.F_Marriage = item.F_Marriage;
                    user.F_BachelorId = item.F_BachelorId;
                    user.F_Education = item.F_Education;
                    user.F_School = item.F_School;
                    user.F_Nationality = item.F_Nationality;
                    user.F_Provid = item.F_Provid;
                    user.F_Address = item.F_Address;
                    user.F_MobilePhone = item.F_MobilePhone;
                    user.F_Email = item.F_Email;
                    user.F_WeChat = item.F_WeChat;
                  
                    user.F_Description = item.F_Description;
                    user.F_PoliticalId = item.F_PoliticalId;
                    user.F_Major = item.F_Major;
                    user.F_BachelorId = item.F_BachelorId;
                    user.F_EmContactPerson = item.F_EmContactPerson;
                    user.F_EmContactPhone = item.F_EmContactPhone;
                    user.F_EduExperience = item.F_EduExperience;
                    user.F_WorkExperience = item.F_WorkExperience;
                    user.F_HomeExperience = item.F_HomeExperience;
                    user.F_Redate = DateTime.Now;
                    user.F_CompanyId=item.F_CompanyId;
                    user.F_OrganizeId=item.F_OrganizeId;
                    user.F_DutyId = item.F_DutyId;
                    user.F_DeleteMark=false;
                    user.F_WorkState = 0;
                    user.F_IsAdmin = false;
               
                    list.Add(user);
                  

                }

                Db.Insertable(list).ExecuteCommand();

            }
        }
        #endregion

    }
}
